//
//  SRMeViewController.m
//  SportRing
//
//  Created by elwin on 10/31/14.
//  Copyright (c) 2014 HotOne. All rights reserved.
//

#import "SRMeViewController.h"
#import "SRMeView.h"
#import "SRAccountNetService.h"
#import "SRMe+Cate.h"
#import "Utils.h"
#import "SRBatteryService.h"
#import "SRDB.h"

@interface SRMeViewController ()

@property (nonatomic, weak)IBOutlet SRMeView *srMeView;

@end

@implementation SRMeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    
    [self getPersonalProfile];
    [self readBattery];

}

- (void)readBattery
{
    [[SRBatteryService readBatteryFromDevice] subscribeNext:^(id x) {
        SRDevice * device = [SRDevice findDeviceForMe:[SRMe currentUser]];
        [_srMeView setBattery:device.battery.intValue];

    } error:^(NSError *error) {
        
    }];
}

- (void)setupData
{
    SRMe * me = [SRMe currentUser];
    SRMeHeaderCellModel *model = [[SRMeHeaderCellModel alloc] initWithHeader:[UIImage imageNamed:@"select_gender_male"]
                                                                    userName:[SRMe currentUser].nickName
                                                                    distance:[NSString stringWithFormat:@"%.1f",[me allDistance] / 5000000]
                                                                     kalorie:[NSString stringWithFormat:@"%d",(int)[me allCalorie]]
                                                                       steps:[NSString stringWithFormat:@"%ld",[me allSteps]]];
    
    _srMeView.srMeHeaderCellModel = model;
}

#pragma mark - UI
- (void)go:(NSString *)ui_interface_string
{
    UIStoryboard *SportRingMainBoard = [UIStoryboard storyboardWithName:[SRSourceConfig uiMainStoryBoard] bundle:nil];
    UIViewController *next = [SportRingMainBoard instantiateViewControllerWithIdentifier:ui_interface_string];
    [self.navigationController pushViewController:next animated:YES];
}

#pragma mark - service

- (void)getPersonalProfile
{
    [SVProgressHUD show];

    [[SRAccountNetService getProfile] subscribeNext:^(id x) {
        
        [SVProgressHUD dismiss];
        [self setupData];

    } error:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"获取数据失败"];

    }];
}

#pragma mark - SRMeViewDelegate

-(void)switchValue:(BOOL)value type:(SR_SWITCH_TYPE)type
{
    NSLog(@"swithcValue:%d type:%d",value,type);
}

-(void)quitLogin
{
    [Utils deleteUserLoginToken:[SRMe currentUser]];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void)gotoPersonalDataViewController
{
    [self go:@"PersonalDataViewController"];
}

-(void)gotoMyHandRingViewController
{
    [self go:@"MyHandRingViewController"];
}

-(void)gotoSilenceClockViewController
{
    [self go:@"SilenceClockViewController"];

}

-(void)gotoCommentViewController
{
    [self go:@"CommentViewController"];

}

-(void)gotoAboutViewController
{
    [self go:@"AboutViewController"];
}

@end
